//
//  SERegistrationDateController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/6.
//

import UIKit

class SERegistrationDateController: SEBaseViewController {

    @IBOutlet weak var dateView: SERegistrationDateView! {
        didSet {
            dateView.selectClosure = {model in
                print(model.time)
                self.viewModel.currentDateModel = model
                self.requestRegisterDoctorList()
            }
        }
    }

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 30))
            tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -30, right: 0)
            tableView.register(UINib(nibName: SERegistrationDoctorDateCell.className, bundle: nil), forCellReuseIdentifier: SERegistrationDoctorDateCell.reuseId)
            tableView.register(UINib(nibName: SERegistrationRecommendDoctorCell.className, bundle: nil), forCellReuseIdentifier: SERegistrationRecommendDoctorCell.reuseId)
            tableView.se_empty = SEEmptyView{ [weak self] in self?.requestRegisterDoctorList()}
        }
    }

    var viewModel = SERegistrationDateViewModel()

    init(with hospitalId: String, departmentId: String) {
        viewModel.hospitalId = hospitalId
        viewModel.departmentId = departmentId
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        requestDateArray()
    }

    override func setupUI() {
        super.setupUI()
        viewModel.dateHandler = { [weak self] status in
            switch status {
            case .success:
                guard let ws = self else { return }
                ws.dateView.models = ws.viewModel.dateModels
            case .auth(let msg):
                self?.presentLoginViewController(selector: #selector(self?.requestDateArray), handle: {
                    SEHUD.showError(message: msg)
                })
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }

        viewModel.updateData = { [weak self] status in
            switch status {
            case .success:
                self?.tableView.se_empty?.allowShow = true
                self?.tableView.reloadData()
            case .auth(let msg):
                self?.presentLoginViewController(selector: #selector(self?.requestRegisterDoctorList), handle: {
                    SEHUD.showError(message: msg)
                })
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    // MARK: - Navi Push

    private func pushChooseNumViewController(model: SERegistrationDoctorShift) {
        let vc = SERegistrationChooseNumController(with: model)
        vc.title = model.doctorName
        navigationController?.pushViewController(vc, animated: true)
    }

    private func pushDoctorDetailViewController(doctorId: String) {
        let vc = SEDoctorDetailViewController(with: doctorId)
        navigationController?.pushViewController(vc, animated: true)
    }

}

// MARK: - UITableViewDataSource,UITableViewDelegate

extension SERegistrationDateController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.numberOfRows(section: section)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.numberOfSection
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == viewModel.lastSection {
            let cell = tableView.dequeueReusableCell(withIdentifier: SERegistrationRecommendDoctorCell.reuseId, for: indexPath) as! SERegistrationRecommendDoctorCell
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: SERegistrationDoctorDateCell.reuseId, for: indexPath) as! SERegistrationDoctorDateCell
        cell.updateCell(model: viewModel.doctorShift(indexPath: indexPath))
        cell.dateViewDelegate = self
        return cell
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return viewModel.heightForRow(indexPath: indexPath)
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return viewModel.heightForHeader(section: section)
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == viewModel.lastSection {
            let bgView = UIView()
            bgView.backgroundColor = UIColor.clear
            let view = SEHospitalDetailSectionHeader()
            view.title = "线上医生推荐"
            bgView.addSubview(view)
            view.snp.makeConstraints { (make) in
                make.left.right.bottom.equalToSuperview()
                make.top.equalTo(10)
            }
            return bgView
        }
        let view = SERegistrationDoctorView()
        view.deleagte = self
        view.updateView(model: viewModel.doctorModel(section: section))
        return view
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return viewModel.heightForFooter(section: section)
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let doctorModel = viewModel.doctorModel(section: section)
        let doctorShift = doctorModel.shiftList
        let view = SERegistrationDoctorFooterView()
        if doctorShift.count <= 2 {
            view.arrowBtn.isHidden = true
            view.delegate = nil
        } else {
            view.arrowBtn.isHidden = false
            view.delegate = self
            view.updateView(model: viewModel.doctorModel(section: section), section: section)
        }
        return view
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
}

// MARK: - SERegistrationDoctorFooterViewDelegate

extension SERegistrationDateController: SERegistrationDoctorFooterViewDelegate {
    func registrationDoctorFooterViewArrowBtnAction(section: Int) {
        let isOpen = viewModel.doctorModel(section: section).isOpen
        viewModel.doctorListModels[section].isOpen = !isOpen
        tableView.reloadData()
    }
}

// MARK: - SERegistrationDoctorViewDelegate

extension SERegistrationDateController: SERegistrationDoctorViewDelegate {
    func registrationDoctorViewTapAction(doctorModel: SERegistrationDoctorModel) {
        guard !doctorModel.inquirydocId.isEmpty else { return }
        pushDoctorDetailViewController(doctorId: doctorModel.inquirydocId)
    }
}

// MARK: - SERegistrationDoctorDateViewDelegate

extension SERegistrationDateController: SERegistrationDoctorDateViewDelegate {
    func registrationDoctorDateViewBtnAction(model: SERegistrationDoctorShift) {
        pushChooseNumViewController(model: model)
    }
}

// MARK: - APIRequset

extension SERegistrationDateController {
    @objc func requestDateArray() {
        viewModel.requestDateArray()
    }

    @objc func requestRegisterDoctorList() {
        viewModel.requestRegisterDoctorList()
    }
}
